package DAOS;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Hibernate.HibernateUtil;
import Negocio.Medico;
import Negocio.PlanCobertura;

public class PlanCoberturaDAO {

	private static PlanCoberturaDAO instancia = null;
	
	private static SessionFactory sf = null;

	public static PlanCoberturaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new PlanCoberturaDAO();
		} 
		return instancia;
	}
	
	public void guardarPlanCobertura(PlanCobertura p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(p);
		session.flush();
		session.getTransaction().commit();
		session.close();

	}


	public void eliminarPlanCobertura(PlanCobertura p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(p);
		session.flush();
		session.getTransaction().commit();
		session.close();
		

	}


	public void modificarPlanCobertura(PlanCobertura p){

		Session session = sf.openSession();
		session.beginTransaction();
		session.update(p);
		session.flush();
		session.getTransaction().commit();
		session.close();

	}


	public List<Object[]> listarPlanesCobertura() {

		Session session = sf.openSession();
		Query q = session.createQuery
		(
				"SELECT p.nombre, p.cobertura" +
				"FROM PlanCobertura p"
		);
		
		@SuppressWarnings("unchecked")
		List<Object[]> planesCobertura = new ArrayList<Object[]>(q.list());
		session.close();
		
		return planesCobertura;
		
	}
	
	
	public List<PlanCobertura> cargarPlanesCobertura () {
		Session session = sf.openSession();
		Query q = session.createQuery(
				"SELECT p.idPlan, p.nombre, p.cobertura" +
				"FROM PlanCobertura p"
		);
		
		@SuppressWarnings("unchecked")
		List<PlanCobertura> pp = new ArrayList<PlanCobertura>(q.list());
		
		session.close();
		
		return pp;
		
	}
	public PlanCobertura cargarUnPlanCobertura (String nombre) {
		PlanCobertura p = new PlanCobertura();
		Session session = sf.openSession();
		session.load(p, nombre);
		session.close();
	
		return p;
	}
}
